package com.sunhao.application;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sunhao.application.util.ViewUtil;

public class MainActivity4 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        EditText phone = findViewById(R.id.phone);
        EditText password = findViewById(R.id.password);

         TextView tv_alert = findViewById(R.id.tv_alert);

        phone.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            //编辑框文本输入之后
            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                if (str.length() == 11) {
                    ViewUtil.hideOneInputMethod(MainActivity4.this, phone);
                }
            }


        });

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                if (str.length() == 6) {
                    ViewUtil.hideOneInputMethod(MainActivity4.this, password);
                }


            }
        });


        Button alert = findViewById(R.id.alert);
        alert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                创建提醒对话框建造器
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity4.this);

//                设置对话框的标题的标题文本
                builder.setTitle("尊敬的用户");

//                设置对话框的肯定按钮文本及其点击事件
                builder.setMessage("你真的要卸载我吗?");

                builder.setPositiveButton("残忍卸载",((dialog, which) -> {
                        tv_alert.setText("虽然依依不舍，但是只能离开了");
                }));

                builder.setNegativeButton("我再想想",(((dialog, which) -> {
                    tv_alert.setText("我将继续和你度过每一天");
                })));

                //根据构造器构建提醒对话框
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }
}